﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

namespace MrPP.SerriedCabinet
{
    public class ScreenText : MonoBehaviour
    {
        [SerializeField]
        private Text _temperatureUI;

        [SerializeField]
        private float _temperature = 0;

        public float temperature {
            set {
                _temperature = value;
            }
            
        }

        [SerializeField]
        private Text _humidityUI;

        [SerializeField]
        private float _humidity = 0;

        [SerializeField]
        private string _room = "room1";

        [SerializeField]
        private Text _roomUI;
        [SerializeField]
        private string _area = "area";

        [SerializeField]
        private Text _areaUI;

        public float humidity
        {
            set
            {
                _humidity = value;
            }

        }
        private void setup() {

         
           setup(CabinetGroup.Instance.getTemperature(), CabinetGroup.Instance.getHumidity(), CabinetGroup.Instance.data.room, CabinetGroup.Instance.data.area);


        }

        public void setup(float temperature, float humidity, string room, string area) {
            _temperature = temperature;
            _humidity = humidity;
            _room = room;
            _area = area;
            refresh();
        }
    

        void Start() {
            if (CabinetGroup.Instance.initialized) {
                setup();
            }
            CabinetGroup.Instance.onSetup += setup;
            TemperatureAndHumidityFactory.Instance.onRecevie += refresh;
        }
        void OnDestroy() {
            if (TemperatureAndHumidityFactory.IsInitialized)
            {
                TemperatureAndHumidityFactory.Instance.onRecevie -= refresh;
            }
            if (CabinetGroup.IsInitialized)
            {
                CabinetGroup.Instance.onSetup -= setup;
                //Model.Instance.onSetup -= setup;
            }
        }
        public void refresh(TemperatureAndHumidityFactory.Data data) {
            _temperature = data.getTemperature();
            _humidity = data.getHumidity();
            refresh();

        }
        public void refresh() {
            _temperatureUI.text = _temperature.ToString("F1") + "'C";
            _humidityUI.text = _humidity.ToString("F0") + "%";
            _roomUI.text = _room;
            _areaUI.text = _area;
        }
    }
}